package com.yxq.mdt.app.modules.personal.ui.activity;

import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.modules.personal.adapter.AddressListAdapter;
import com.yxq.mdt.app.modules.personal.bean.AddressBean;
import com.yxq.mdt.app.modules.personal.bean.AddressListBean;
import com.yxq.mdt.app.modules.personal.model.AddressModel;
import com.yxq.mdt.app.modules.personal.presenter.AddressPresenter;
import com.yxq.mdt.app.modules.personal.presenter.IAddressContract;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author L
 * @time
 * @describe 收货地址
 */
public class MainPersonalShippingAddressAc extends BaseActivity<AddressPresenter> implements IAddressContract.View {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.address_btn_add)
    Button addressBtnAdd;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    AddressListAdapter addressListAdapter;
    private static final int REQUEST_CODE = 10086;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_personal_shipping_address;
    }

    @Override
    public void initView() {
        mPresenter = new AddressPresenter(this, new AddressModel());
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> mPresenter.list());
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        addressListAdapter = new AddressListAdapter(R.layout.layout_address_list_layout, null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(addressListAdapter);
        addressListAdapter.setEmptyView(R.layout.layout_empty_address_view, recyclerView);
        addressListAdapter.isUseEmpty(false);
        addressListAdapter.getEmptyView().findViewById(R.id.empty_btn_go_add_address).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mActivity, AddressManagerAddActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
        addressListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.btnDelete://删除
                    deleteAlert(position);
                    break;
                case R.id.address_iv_edit://编辑地址
                    Intent intent = new Intent(mActivity, AddressManagerAddActivity.class);
                    intent.putExtra("data", addressListAdapter.getItem(position));
                    startActivityForResult(intent, REQUEST_CODE);
                    break;
                default://返回给上一界面选中的地址
                    if (getIntent().getStringExtra("from") != null) {
                        Intent data = new Intent();
                        data.putExtra("data", addressListAdapter.getItem(position));
                        setResult(RESULT_OK, data);
                        finish();
                    } else {
                        intent = new Intent(mActivity, AddressManagerAddActivity.class);
                        intent.putExtra("data", addressListAdapter.getItem(position));
                        startActivityForResult(intent, REQUEST_CODE);
                    }
                    break;
            }
        });
        refreshLayout.autoRefresh();
    }

    /**
     * 删除地址
     *
     * @param position
     */
    private void deleteAlert(int position) {
        CostumerDialog dialog = new CostumerDialog.Builder(this, R.style.custom_dialog_no_match)
                .setContentView(R.layout.dialog_confirm_cancel_layout)
                .setCancelable(true)
                .create();

        TextView title = dialog.findViewById(R.id.tv_title);

        title.setText("温馨提示");

        TextView content = dialog.findViewById(R.id.tv_content);
        content.setText("确定删除此地址吗？");

        //继续
        TextView sure = dialog.findViewById(R.id.tv_sure);
        sure.setText("确定");
        sure.setOnClickListener(v -> {
            dialog.dismiss();
            deleteAddress(position);
        });

        //取消
        TextView cancel = dialog.findViewById(R.id.tv_cancel);
        cancel.setText("取消");
        cancel.setOnClickListener(v -> {
            dialog.dismiss();
        });

        dialog.show();
    }

    private void deleteAddress(int position) {
        mPresenter.delete(position, addressListAdapter.getItem(position).getId());
    }

    @OnClick(R.id.address_btn_add)
    public void onViewClicked() {
        Intent intent = new Intent(mActivity, AddressManagerAddActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    public void onLoadSuccess(List<AddressBean> addressBeanList) {

    }

    @Override
    public void onLoadFailed(String msg) {
        ToastUtils.showShort(msg);
    }

    @Override
    public void onLoadListSuccess(AddressListBean addressListBean) {
        refreshLayout.finishRefresh();
        addressListAdapter.setNewData(addressListBean.getList());
        if (getIntent().getStringExtra("from") != null && addressListAdapter.getData() != null && addressListAdapter.getData().size() == 0) {
            Intent intent = new Intent(mActivity, AddressManagerAddActivity.class);
            intent.putExtra("from", "order");
            startActivityForResult(intent, REQUEST_CODE);
            finish();
            return;
        }
        addressListAdapter.isUseEmpty(!(addressListAdapter.getData().size() > 0));//是否显示缺省页
        addressBtnAdd.setVisibility((!(addressListAdapter.getData().size() > 0) ? View.GONE : View.VISIBLE));//是否显示底部添加地址按钮
    }

    @Override
    public void onLoadListFailed(String msg) {
        refreshLayout.finishRefresh();
        ToastUtils.showShort(msg);
    }

    @Override
    public void onSaveSuccess(AddressListBean.ListBean result) {

    }

    @Override
    public void onSaveFailed(String msg) {

    }

    @Override
    public void onDeleteSuccess(int position, String result) {
        addressListAdapter.remove(position);
        addressListAdapter.isUseEmpty(!(addressListAdapter.getData().size() > 0));
        addressBtnAdd.setVisibility((!(addressListAdapter.getData().size() > 0) ? View.GONE : View.VISIBLE));
    }

    @Override
    public void onDeleteFailed(String msg) {
        ToastUtils.showShort(msg);
    }

    @Override
    public void onSetDefaultSuccess(String result) {

    }

    @Override
    public void onSetDefaultFailed(String msg) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_CODE://添加地址成功，自动刷新列表
                refreshLayout.autoRefresh();
                break;
        }
    }
}
